package pl.bigk.utils.lexengines;

import java.util.Locale;
import java.util.ResourceBundle;

/**
 * Implementation of {@link LexEngine} which converts given number to polish
 * text.
 * 
 * @author lukasz.kalek
 */
public class LexEnginePL implements LexEngine {

    public static final Locale LOCALE_PL = new Locale("PL", "pl");
    
    public static final String BASE_NAME =
        "pl.bigk.utils.lexengines.resources.LexEngine";
    
    private static ResourceBundle bundle = ResourceBundle.getBundle(BASE_NAME, LOCALE_PL);
    
    /**
     * 0-argument constructor.
     */
    public LexEnginePL() {
        super();
    }

    /* (non-Javadoc)
     * @see pl.bigk.utils.lexengines.LexEngine#getTextForNumber(long)
     */
    public String getTextForNumber(long number) {
        return getTextForNumber(Long.toString(number));
    }
    
    private String getTextForNumber(String number) {
        
        String txt = null;
        
        if (number.length() == 1) {
            txt = get1Digit(number);
        } else if (number.length() == 2) {
            txt = get2Digits(number);
        } else if (number.length() == 3) {
            txt = get3Digits(number);
        } else if (number.length() > 3 && number.length() <= 6 ) { // 1000 ... 999999
            txt = getThousands(number);
        } else if (number.length() > 6 && number.length() <= 9) { // 1000000 ... 999999999
            txt = getMilions(number);
        }
        
        return txt;
    }
    
    private String get1Digit(String number) {
        return get1Digit(number, true);
    }

    private String get1Digit(String number, boolean return0) {
        if ("0".equals(number) && !return0) {
            return "";
        } else {
            return bundle.getString(number);
        }
    }
    
    private String get2Digits(String number) {
        
        long digits2 = Long.parseLong(number);
        if (digits2 >= 10) {
            if (digits2 <= 20) {
                return bundle.getString(number);
            } else {
                long digit2 = Long.parseLong(number);
                String txt = bundle.getString(Long.toString(digit2 / 10) + "0");
                
                long digit1 = digit2 % 10;
                if (digit1 != 0) {
                    txt += " " + bundle.getString(Long.toString(digit1));
                }
                
                return txt;            
            }
        } else {
            return get1Digit(Long.toString(digits2), false);
        }
    }
    
    private String get3Digits(String number) {
        
        String res = null;
        long digits3 = Long.parseLong(number);
        if (digits3 >= 100) {
            res = bundle.getString(Long.toString(digits3 / 100) + "00");
            
            long digits2 = (digits3 % 100);
            if (digits2 > 9) {
                res += " " + get2Digits(Long.toString(digits2));
            } else if (digits2 > 0) {
                res += " " + get1Digit(Long.toString(digits2));
            }
        } else {
            res = get2Digits("" + digits3);
        }
        
        return res;
    }
    
    private String getThousands(String number) {
        
        String res = "";
        
        String digits3 = number.substring(number.length() - 3);
        String thousands = number.substring(0, number.length() - 3);

        long thousandsLong = Long.parseLong(thousands);
        String wKey = null;
        if (thousandsLong < 10) {
            res = get1Digit(thousands);
            wKey = "03_" + thousandsLong;
        } else if (thousandsLong < 100) {
            res = get2Digits(thousands);
            wKey = "03_0";
        } else {
            res = get3Digits(thousands);
            wKey = "03_0";
        }
        res += " " + bundle.getString(wKey);
        
        String tail = get3Digits(digits3);
        if (tail.length() > 0) {
            res += " " + tail;
        }
        return res;
    }

    private String getMilions(String number) {
        
        String res = "";
        
        String thousands = number.substring(number.length() - 6);
        String milons = number.substring(0, number.length() - 6);

        long milionsLong = Long.parseLong(milons);
        String wKey = null;
        if (milionsLong < 10) {
            res = get1Digit(milons);
            wKey = "06_" + milionsLong;
        } else if (milionsLong < 100) {
            res = get2Digits(milons);
            wKey = "06_0";
        } else {
            res = get3Digits(milons);
            wKey = "06_0";
        }
        res += " " + bundle.getString(wKey);

        String tail = null;
        long thousandsLong = Long.parseLong(thousands);
        thousands = Long.toString(thousandsLong);
        if (thousandsLong > 999) {
        	tail = getThousands(thousands);
        } else {
        	tail = get3Digits(thousands);
        }
        
        if (tail != null && tail.length() > 0) {
            res += " " + tail;
        }
        return res;
    }

    public static void main(String[] args) {
        LexEnginePL lPl = new LexEnginePL();
        System.out.println(lPl.getTextForNumber(234000));
    }
}
